package com.wedimob.simpleslides;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.wedimob.slideshare.RuntimeConfig;
import com.wedimob.slideshare.Search;
import com.wedimob.slideshare.SearchCriteria;
import com.wedimob.slideshare.SortBy;
import com.wedimob.slideshare.WriteType;
import com.wedimob.slideshare.task.Monitor;

public class MainActivity extends Activity {

	EditText editText;
	Button searchBouton;
	TextView progressValue;
	ProgressBar progressBar;

	public EditText getEditText() {
		return editText;
	}

	public Button getSearchBouton() {
		return searchBouton;
	}

	public TextView getProgressValue() {
		return progressValue;
	}

	public ProgressBar getProgressBar() {
		return progressBar;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (editText == null)
			editText = (EditText) findViewById(R.id.search_text);
		if (searchBouton == null)
			searchBouton = (Button) findViewById(R.id.search_button);
		if (progressValue == null)
			progressValue = (TextView) findViewById(R.id.progress_text);
		if (progressBar == null)
			progressBar = (ProgressBar) findViewById(R.id.progress_bar);

		searchBouton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				progressValue.setText("0 %");
				progressBar.setProgress(0);
				Toast.makeText(getApplicationContext(),
						"Search " + editText.getText().toString(),
						Toast.LENGTH_LONG).show();

				String url = Environment.getExternalStorageDirectory()
						.getPath();
				if (android.os.Build.DEVICE.contains("Samsung")
						|| android.os.Build.MANUFACTURER.contains("Samsung")) {
					url = url + "/external_sd/";
				}

				RuntimeConfig.getInstance().setOutputdir(url + "/slideshow");
				Search.searchDocuments(new SearchCriteria(editText.getText()
						.toString(), SortBy.POPULARITY, 1), WriteType.ONDISK,
						new ProgressListener(MainActivity.this));
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private static class ProgressListener implements Observer {

		private MainActivity activity;

		public ProgressListener(MainActivity activity) {
			super();
			this.activity = activity;
		}

		@Override
		public void update(Observable obj, Object msg) {
			if (obj instanceof Monitor) {
				System.out.println("Progress : "
						+ Math.round(10000.0 * (Double) msg) / 100 + " %");

				final int value = (int) Math.round(10000.0 * (Double) msg) / 100;
				activity.getProgressBar().post(new Runnable() {

					@Override
					public void run() {
						activity.getProgressValue().setText(value + " %");
						activity.getProgressBar().setProgress(value);
					}
				});

			}
		}
	}
}
